<?php
/**
 * 首页 dashboard 面板数据
 * Author: lyun
 * Date: 2018/3/30
 * Created by Panxsoft.
 */

namespace App\Http\Controllers\Admin\Store;

use App\Http\Controllers\ApiController;
use App\Http\Requests\Admin\Customer\Store\StoreIndexRequest;
use App\Models\OpenWechat\WechatUser;
use App\Models\Panx\Product;
use App\Models\Panx\Visit;
use App\Models\Store\Store;
use App\Services\Assemblers\Admin\DashboardAssembler;
use App\Services\Repositories\Product\ProductRepository;
use App\Services\Repositories\SkinRecord\SkinRecordCountRepository;
use App\Services\Repositories\SkinRecord\SkinRecordRepository;
use App\Services\Repositories\VisitRepository;
use App\Services\Utils\Guard;
use App\Services\Utils\PanxCollection;
use Illuminate\Database\Eloquent\Builder;
use Illuminate\Database\Eloquent\Collection;
use Illuminate\Http\Request;


class DashboardController extends ApiController
{
    const ROUTES = [
        'index'         => 'api.admin.store.dashboard.index',
        'skin-counts'   => 'api.admin.store.dashboard.skin.counts',
        'device-counts' => 'api.admin.store.dashboard.device.counts',
    ];

    /**
     * 首页 dashboard 面板数据
     * @test
     *
     * @param StoreIndexRequest $request
     * @return array|\Illuminate\Http\Response
     * @throws \Exception
     */
    public function index(StoreIndexRequest $request)
    {
        $store = Guard::storeAdmin()->store;

        $wechat_user_profiles = [];
        foreach ( $this->queryWechatUsersBuilder($store)->cursor() as $wechat_user )
        {
            $wechat_user_profiles [] = $wechat_user;
        }
        $wechat_user_profiles = collect($wechat_user_profiles);

        $profiles = DashboardAssembler::assembleWxUserProfiles($wechat_user_profiles);

        $products = (new ProductRepository())->countStoreTryOn($store);

        return $this->response(compact('products', 'profiles'));
    }

    /**
     * @deprecated (使用下面的：getStoreSkinCounts（）)
     *  返回 访客统计数据
     *
     * @param Request $request
     * @return array|\Illuminate\Http\Response
     * @throws \Exception
     */
    public function getVisitCounts(Request $request)
    {
        $store = Guard::storeAdmin()->store;

        $group_type = $request->group_type?:'month';
        $store_ids = [$store->id];

        $visit_counts = (new VisitRepository)->queryCustomerOrStoreVisits($store_ids, $group_type);

        return $this->response(compact('visit_counts'));
    }

    /**
     * 返回 当前店铺的测肤统计数据
     * @test
     *
     * @param Request $request
     * @return array|\Illuminate\Http\Response
     * @throws \Exception
     */
    public function getStoreSkinCounts(Request $request)
    {
        $store = Guard::storeAdmin()->store;

        $group_type = $request->group_type?:'month';
        $store_ids = [$store->id];

        $skin_record_counts = (new SkinRecordRepository())->queryCustomerOrStoreSkinRecords($store_ids, $group_type);


        return $this->response(compact('skin_record_counts'));
    }

    /**
     * 返回 店铺下各设备的测肤统计数据
     * @test
     *
     * @param Request $request
     * @return array|\Illuminate\Http\Response
     */
    public function getStoreDeviceAccountSkinCounts(Request $request)
    {
        /** @var Store $store */
        $store = Guard::storeAdmin()->store;

        $store_ids = [$store->id];

        $device_account_count_skin = (new SkinRecordCountRepository)->deviceAccountCountSkin($store_ids);

        return $this->response(compact('device_account_count_skin'));

    }

    /**
     * @param Store $store
     * @return Builder
     */
    private function queryWechatUsersBuilder(Store $store)
    {
        $store_id = $store->id;

        /** @var Builder $wechat_users */
        $wechat_users = $store->wechatUsers()
            ->whereHas('skinRecords', function(Builder $query) use($store_id){
                return $query->where('store_id', $store_id);
            });

        return $wechat_users;
    }
}
